package com.debao;

import com.debao.common.core.controller.BaseController;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.core.page.TableDataInfo;
import com.debao.common.enums.OrderStatusEnum;
import com.debao.common.utils.SecurityUtils;
import com.debao.system.domain.VipcardOrder;
import com.debao.system.service.IVipCardService;
import com.debao.system.service.IVipcardOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Objects;

@RestController
@RequestMapping("/api/vipCardOrder")
@CrossOrigin
public class ApiVipCardOrder extends BaseController {


    @Autowired
    private IVipcardOrderService vipcardOrderService;

    @Autowired
    private IVipCardService vipCardService;
    /**
     * 会员卡订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(VipcardOrder vipcardOrder) {
        vipcardOrder.setUserId(SecurityUtils.getUserId());
        startPage();
        return getDataTable(vipcardOrderService.selectVipcardOrderList(vipcardOrder));
    }
    /**
     * 会员卡订单列表
     */
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable("id") Long id) {
        return success(vipcardOrderService.selectVipcardOrderById(id));
    }

    /**
     * 订单是否支付成功
     *
     * @param orderNo
     * @return
     */
    @GetMapping("/isPaySuccess/{orderNo}")
    public AjaxResult isPaySuccess(@PathVariable("orderNo") String orderNo) {
        return AjaxResult.success(vipcardOrderService.isPaySuccess(orderNo));
    }

    /**
     * 取消支付
     */
    @PostMapping("/cancelPay/{orderNo}")
    public AjaxResult cancelPay(@PathVariable("orderNo") String orderNo) {
        VipcardOrder vipcardOrder = vipcardOrderService.selectVipCardOrderByOrderNo(orderNo);
        if (Objects.isNull(vipcardOrder)) {
            return error("订单不存在");
        }
        if (!vipcardOrder.getStatus().equals(OrderStatusEnum.None.name())) {
            return error("订单已处理");
        }
        vipcardOrder.setStatus(OrderStatusEnum.Cancel.name());
        int row = vipcardOrderService.updateVipcardOrder(vipcardOrder);
        if (row == 1) {
            return AjaxResult.success("取消成功");
        }
        return error("取消失败");
    }
}
